/**
 * @(#)SimulatorTaskVerDAOImpl.java 2016年12月1日
 * Copyright(C) 2016 ZHEXIN IT CO.,LTD. All rights reserved.
 */
package org.jsbd.boss.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.jsbd.boss.common.redis.RedisClient;
import org.jsbd.boss.dao.IThirdSmsConfigDAO;
import org.jsbd.boss.domian.channel.ThirdSmsConfig;
import org.jsbd.boss.domian.channel.ThirdSmsSyncVo;
import org.jsbd.boss.service.IThirdSmsConfigService;
import org.jsbd.boss.util.CacheKey;
import org.jsbd.boss.util.DateUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import edu.hziee.common.tcp.cache.CachedHolder;

/**
 * 模拟器版本号
 * 
 * @author <a href="mailto:xupengcheng@zhexinit.com" >徐鹏程</a>
 * @version 1.0.0
 */
@Service
public class ThirdSmsConfigServiceImpl implements IThirdSmsConfigService {
	public static final Logger LOGGER = LoggerFactory.getLogger(ThirdSmsConfigServiceImpl.class);

	@Resource
	private IThirdSmsConfigDAO thirdSmsConfigDao;

	@Resource
	private RedisClient redisClient;

	@Resource
	private CachedHolder ehcache;

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.jsbd.boss.service.IThirdSmsConfigService#insertThirdSmsConfig(org
	 * .jsbd.boss.domian.channel.ThirdSmsConfig)
	 */
	@Override
	public Integer insertThirdSmsConfig(ThirdSmsConfig thirdSmsConfig) {
		return thirdSmsConfigDao.insertThirdSmsConfig(thirdSmsConfig);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.jsbd.boss.service.IThirdSmsConfigService#updateThirdSmsConfig(org
	 * .jsbd.boss.domian.channel.ThirdSmsConfig)
	 */
	@Override
	public Integer updateThirdSmsConfig(ThirdSmsConfig thirdSmsConfig) {
		return thirdSmsConfigDao.updateThirdSmsConfig(thirdSmsConfig);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.jsbd.boss.service.IThirdSmsConfigService#selectThirdSmsConfig(java
	 * .lang.String)
	 */
	@Override
	public ThirdSmsConfig selectConfig(Integer id) {
		ThirdSmsConfig thirdSmsConfig = new ThirdSmsConfig();
		thirdSmsConfig.setId(id);
		return thirdSmsConfigDao.selectConfig(thirdSmsConfig);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.jsbd.boss.service.IThirdSmsConfigService#selectAllThirdSmsConfig()
	 */
	@Override
	public List<ThirdSmsConfig> selectAllThirdSmsConfigCache() {
		String key = CacheKey.THIRD_SMS_LIST;
		List<ThirdSmsConfig> list = (List<ThirdSmsConfig>) ehcache.get(key);
		List<ThirdSmsConfig> targetList = new ArrayList<ThirdSmsConfig>();
		if (list == null) {
			list = (List<ThirdSmsConfig>) redisClient.get(key);
			if (list != null) {
				ehcache.put(key, list);
			}
		}
		if (list == null) {
			ThirdSmsConfig config = new ThirdSmsConfig();
			config.setIsUseful(1);
			list = thirdSmsConfigDao.selectThirdSmsList(config);
			if (list == null || list.size() == 0) {
				list = new ArrayList<ThirdSmsConfig>();
			}
			redisClient.put(key, list, 300);
			// 记录apk对应的短信规则
			for (ThirdSmsConfig tempConfig : list) {
				String apk = "third" + tempConfig.getCode();
				ThirdSmsSyncVo syncVo = new ThirdSmsSyncVo();
				syncVo.setCallbackUrl(tempConfig.getCallbackUrl());
				syncVo.setRegexContent(tempConfig.getRegexContent());
				syncVo.setCode(tempConfig.getCode());
				syncVo.setTaskType(tempConfig.getTaskType());
				syncVo.setMsgContent(tempConfig.getMsgContent());
				redisClient.put(apk, syncVo, 300);
			}
		}
		targetList.addAll(list);
		return targetList;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.jsbd.boss.service.IThirdSmsConfigService#selectAllThirdSmsConfig()
	 */
	@Override
	public List<ThirdSmsConfig> selectPageList(ThirdSmsConfig thirdSmsConfig) {
		List<ThirdSmsConfig> list = thirdSmsConfigDao.selectPageList(thirdSmsConfig);
		for (ThirdSmsConfig config : list) {
			String taskNum = config.getTaskNum() == null ? "0" : config.getTaskNum().toString();
			String taskRecordKey = CacheKey.THIRD_TASK_HASH + config.getCode() + DateUtil.getDaysOfTime();
			String execTotal = redisClient.evalHget(taskRecordKey, "exectotal");
			String succexeTotal = redisClient.evalHget(taskRecordKey, "succexectotal");
			if (execTotal == null) {
				execTotal = "0";
			}
			if (succexeTotal == null) {
				succexeTotal = "0";
			}

			config.setViewTaskNum(taskNum + "|" + execTotal + "|" + succexeTotal);
		}

		return list;
	}


	@Override
	public Integer selectCount(ThirdSmsConfig thirdSmsConfig){
		return thirdSmsConfigDao.selectCount(thirdSmsConfig);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.jsbd.boss.service.IThirdSmsConfigService#delThirdSmsConfig(java.lang
	 * .String)
	 */
	@Override
	public int delThirdSmsConfig(Integer id) {
		return thirdSmsConfigDao.delThirdSmsConfig(id);
	}
	

}
